/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package enteties.person;

import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import util.person.LanguageDetails;

/**
 *
 * @author Gebruiker
 */
@Stateless
public class LanguageFacade extends AbstractFacade<Language> implements LanguageFacadeLocal {
    @PersistenceContext(unitName = "Liebaut-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public LanguageFacade() {
        super(Language.class);
    }
    
    @Override
    public Integer createLanguage(String name, String languageCode){
        Language language = new Language(name, languageCode);
        create(language);
        return language.getId();
    }
    
    @Override
    public void editLanguage(Integer id, String name, String languageCode){
        Language language = find(id);
        language.setName(name);
        language.setLanguageCode(languageCode);
        edit(language);
    }
    
    @Override
    public void removeLanguage(Integer id){
        Language language = find(id);
        remove(language);
    }
    
    @Override
    public LanguageDetails getLanguageDetails(Integer id){
        return convert(find(id));
    }
    
    @Override
    public Collection<LanguageDetails> getAllLanguageDetails(){
        Collection<LanguageDetails> languages = new ArrayList<LanguageDetails>();
        for(Language language:findAll()){
            languages.add(convert(language));
        }
        return languages;
    }
    
    public LanguageDetails convert(Language language){
        return new LanguageDetails(language.getId(), language.getName(), language.getLanguageCode());
        
    }
    
}
